//
//  MA.cpp
//  Chart
//
//  Created by ma xiaoyang on 6/18/13.
//
//

#include "MA.h"

using namespace ytx;
using namespace std;


MA::MA(IndexConfig indexConfig):IndexData(indexConfig, 0)
{
    indexName = "MA";
    type = QuotationType::MA;
}

MA::~MA()
{

}

void MA::updateLastest(Quotation * data)
{
    vector<double> nList = para.value;
    int len = data->getDataLength();

    int z=0, n;
    double value;
    for (; z < nList.size(); z++) {
        n = (int) Tools::between(para.range[z].first, para.range[z].second, nList[z]);
        value = computeMA(data, n, &KLineItem::close, len-n)[0].back();
        lineData[z][len-1] = value;
    }
}

void MA::calculate(Quotation * data)
{
    this->lineData.clear();

    vector<int> nList;

    int z= 0;
    for (; z < para.value.size(); z++) {
        nList.push_back( (int)Tools::between(para.range[z].first, para.range[z].second, para.value[z]) );
    }
    drawType = IndexDrawType::KIndex;
    lineData= computeMA(data, nList, &KLineItem::close);
    drawingCount = (int)lineData[0].size();

}


string MA::getCrosshairYData(int index)
{
    return hasData() ? Tools::Convert2FixString(indexData[0][index], toFix) : "";
}

vector<vector<double> > MA::getOriginLineData()
{
    return this->originLineData;
}

void MA::setOriginLineData(vector<vector<double> > originLineData)
{
    this->originLineData = originLineData;
}

int MA::getDataLength() const
{
    return (int)lineData.size() ? (int)lineData[0].size() : 0;
}
